package Dao;

import UnderAccessDBclass.Druid_utils;
import bean.Course;

import java.sql.Date;
import java.util.ArrayList;

public class CourseDao {
    public ArrayList<Course> selectAll() {
        ArrayList<Course> courseList = new ArrayList<>();
        String sql = "select * from course";
        ArrayList<Object[]> list = Druid_utils.executeQuery(sql, null);
        for (Object[] objects : list) {
            Course course = new Course();
            course.setCno((String) objects[0]);
            course.setCname((String) objects[1]);
            course.setCredit(objects[2].toString());
            course.setCtime(objects[3].toString());
            course.setQuota(objects[4].toString());
            courseList.add(course);
        }
        return courseList;
    }

    public Course selectByCno(String cno) {
        Course course = null;
        ArrayList<Course> courseList = new ArrayList<>();
        String sql = "select * from course where cno=?";
        Object[] params = {cno};
        ArrayList<Object[]> list = Druid_utils.executeQuery(sql, params);

        if (list.size() != 0) {
            course = new Course();
            Object[] objects = list.get(0);
            course.setCno((String) objects[0]).setCname((String) objects[1]).setCredit(objects[2].toString());
            course.setCtime(objects[3].toString()).setQuota(objects[4].toString());
        }

        return course;
    }

    public Course selectByCname(String cname){
        Course course = null;
        ArrayList<Course> courseList = new ArrayList<>();
        String sql = "select * from course where cname=?";
        ArrayList<Object[]> list = Druid_utils.executeQuery(sql, new Object[]{cname});

        if (list.size() > 0) {
            course = new Course();
            Object[] objects = list.get(0);
            course.setCno((String) objects[0]);
            course.setCname((String) objects[1]);
            course.setCredit(objects[2].toString());
            course.setCtime(objects[3].toString());
            course.setQuota(objects[4].toString());
        }

        return course;
    }

    public ArrayList<Course> delete(String cno) {
        String sql = "delete from course where cno=?";
        Druid_utils.executeUpdate(sql, new Object[]{cno});
        return selectAll();
    }

    public ArrayList<Course> batchDelete(String[] cno_list) {
        String sql = "delete from course where cno=?";
        for (String cno : cno_list) {
            Druid_utils.executeUpdate(sql, new Object[]{cno});
        }

        return selectAll();
    }

    public ArrayList<Course> insert(String cno, String cname, String credit, String ctime, String quota) {
        String sql = "insert into course values(?,?,?,?,?)";
        Object[] params = {cno, cname, Integer.parseInt(credit), Date.valueOf(ctime), Integer.parseInt(quota)};
        Druid_utils.executeUpdate(sql, params);
        return selectAll();
    }

    public ArrayList<Course> update(String cno, String cname, String credit, String ctime, String quota) {
        String sql = "update course set cname=?,credit=?,ctime=?,quota=? where cno=?";
        Date date = null;
        if (ctime != null) {
            date = Date.valueOf(ctime);
        }
        Object[] params = {cname, credit, date, quota, cno};
        Druid_utils.executeUpdate(sql, params);
        return selectAll();
    }

    public static void main(String[] args) {
        CourseDao courseDao = new CourseDao();
        Course c = courseDao.selectByCno("20740061");
        System.out.println(c);
//        ArrayList<Course> courseList = courseDao.selectAll();
//        for (Course course : courseList) {
//            System.out.println(course);
//        }

        ArrayList<Course> list = courseDao.insert("20740088","test","3","2019-01-01","100");
        for (Course course : list) {
            System.out.println(course);
        }
    }
}
